package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("menu")
public class MenuController {

    @Reference
    private MenuService menuService;
    @Reference
    private UserService userService;
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("findByUser")
    public Result findByUser(String username) {
        try {
            String menu = jedisPool.getResource().get(username);
            if (menu == null) {
                List<Menu> list = menuService.findByUser(username);
                return new Result(true, MessageConstant.GET_MENU_SUCCESS, list);

            } else {
                List<Menu> jsonlist = JSONArray.parseArray(menu, Menu.class);
                return new Result(true, MessageConstant.GET_MENU_SUCCESS, jsonlist);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }
    @RequestMapping("findAll")
    public Result findAll() {
        try {
            List<Menu> list = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }
}